/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package ga.tsp;

import java.util.Iterator;
import java.util.List;

import org.jheuristics.Individual;
import org.jheuristics.ga.DoubleFitness;
import org.jheuristics.ga.Fitness;
import org.jheuristics.ga.FitnessEvaluator;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;

import utils.Graph;

public class TSPFitnessEvaluator implements FitnessEvaluator {
	private RouteEvaluator routeEvaluator;

	/**
	 * TODO
	 *
	 * @param evaluator
	 */
	public TSPFitnessEvaluator(RouteEvaluator routeEvaluator) {
		setRouteEvaluator(routeEvaluator);
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	public RouteEvaluator getRouteEvaluator() {
		return routeEvaluator;
	}

	/**
	 * TODO
	 *
	 * @param routeEvaluator
	 */
	public void setRouteEvaluator(RouteEvaluator routeEvaluator) {
		if (null == routeEvaluator) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		this.routeEvaluator = routeEvaluator;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.FitnessEvaluator#evaluateFitness(org.jheuristics.ga.Individual, org.jheuristics.ga.Population, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public Fitness evaluateFitness(Individual individual, Population population, GAStatus status, GAConfig config) {
		if (null == individual) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		Graph graph = routeEvaluator.getGraph();
		List gens = (List) individual.getGens();
		long sum = 0;

		Object currentPosition = routeEvaluator.getInitialPosition();
		for (Iterator it = gens.iterator(); it.hasNext(); ) {
			Object nextPosition = it.next();
			long longValue = ((Long) graph.getWeigth(currentPosition, nextPosition)).longValue();
			sum += longValue;
			currentPosition = nextPosition;
		}
		sum += ((Long) graph.getWeigth(currentPosition, routeEvaluator.getInitialPosition())).longValue();
		return new TSPFitness(new Long(sum));
	}

	private class TSPFitness implements DoubleFitness {
		private Number value;

		/**
		 * TODO
		 *
		 * @param value
		 */
		public TSPFitness(Number value) {
			this.value = value;
		}

		/* (non-Javadoc)
		 * @see org.jheuristics.ga.DoubleFitness#toDouble()
		 */
		public double toDouble() {
			return -value.doubleValue();
		}

		/**
		 * TODO
		 *
		 * @param obj
		 * @return
		 */
		public int compareTo(Object obj) {
			return -((Comparable) value).compareTo(((TSPFitness) obj).value);
		}
		
	}

}
